package com.ruoyi.system.saleorder.controller;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.saleorder.domain.SaleGoodsAfterReq;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.saleorder.domain.SaleGoodsAfter;
import com.ruoyi.system.saleorder.service.ISaleGoodsAfterService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 售后Controller
 *
 * @author ruoyi
 * @date 2024-06-07
 */
@RestController
@RequestMapping("/saleOrder/after")
public class SaleGoodsAfterController extends BaseController {
    @Autowired
    private ISaleGoodsAfterService saleGoodsAfterService;

    /**
     * 查询售后列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SaleGoodsAfterReq saleGoodsAfter) {
        startPage();
        List<SaleGoodsAfter> list = saleGoodsAfterService.selectSaleGoodsAfterList(saleGoodsAfter);
        if (CollectionUtils.isNotEmpty(list)){
            for (SaleGoodsAfter goodsAfter : list) {
                // 获取当前日期
                LocalDate today = LocalDate.now();
                try {
                    Date createTime = goodsAfter.getCreateTime();
                    LocalDate createDate = createTime.toInstant()
                            .atZone(ZoneId.systemDefault())
                            .toLocalDate();
                    long daysBetween = ChronoUnit.DAYS.between(createDate, today);
                    goodsAfter.setAlongDay(daysBetween);
                }catch (Exception e){
                    goodsAfter.setAlongDay(0L);
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出售后列表
     */
    @Log(title = "售后" , businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SaleGoodsAfterReq saleGoodsAfter) {
        List<SaleGoodsAfter> list = saleGoodsAfterService.selectSaleGoodsAfterList(saleGoodsAfter);
        ExcelUtil<SaleGoodsAfter> util = new ExcelUtil<SaleGoodsAfter>(SaleGoodsAfter.class);
        return util.exportExcel(list, "售后数据");
    }

    /**
     * 获取售后详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(saleGoodsAfterService.selectSaleGoodsAfterById(id));
    }

    /**
     * 新增售后
     */
    @Log(title = "售后" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SaleGoodsAfter saleGoodsAfter) {
        saleGoodsAfter.setCreateId(SecurityUtils.getUserId());
        return toAjax(saleGoodsAfterService.insertSaleGoodsAfter(saleGoodsAfter));
    }

    /**
     * 新增售后
     */
    @Log(title = "售后" , businessType = BusinessType.INSERT)
    @PostMapping("/pushAfter")
    public AjaxResult pushAfter(@RequestBody SaleGoodsAfter saleGoodsAfter) {
        saleGoodsAfter.setCreateId(SecurityUtils.getUserId());
        return AjaxResult.success();
    }

    /**
     * 修改售后
     */
    @Log(title = "售后" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SaleGoodsAfter saleGoodsAfter) {
        return toAjax(saleGoodsAfterService.updateSaleGoodsAfter(saleGoodsAfter));
    }

    /**
     * 转处理
     */
    @Log(title = "转处理" , businessType = BusinessType.UPDATE)
    @PutMapping("/dispose/{id}")
    public AjaxResult dispose(@PathVariable Long id) {
        return toAjax(saleGoodsAfterService.dispose(id));
    }

    /**
     * 转处理
     */
    @Log(title = "" , businessType = BusinessType.UPDATE)
    @PutMapping("/overTask/{id}")
    public AjaxResult overTask(@PathVariable Long id) {
        return AjaxResult.success(saleGoodsAfterService.overTask(id));
    }


    /**
     * 删除售后
     */
    @Log(title = "售后" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(saleGoodsAfterService.deleteSaleGoodsAfterByIds(ids));
    }

}
